package com.sskj.ffex.v.activity;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.sskj.ffex.common.ClassicsHeader;
import com.sskj.ffex.R;
import com.sskj.ffex.common.adapter.BaseAdapter;
import com.sskj.ffex.common.adapter.ViewHolder;
import com.sskj.ffex.common.widget.ToolBarLayout;
import com.sskj.ffex.m.bean.NoticeEntry;
import com.sskj.ffex.m.bean.NoticeList;
import com.sskj.ffex.p.fragment.NoticePresenter;
import com.sskj.ffex.utils.ClickUtil;
import com.sskj.ffex.utils.TimeFormatUtil;
import com.sskj.ffex.v.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * ProjectName：bfex
 * DESC: (类描述)
 * Created by 李岩 on 2018/6/29 0029
 * updateName:(修改人名称)
 * updateTime:(修改时间)
 * updateDesc:(修改内容)
 */
public class InforMationActivity extends BaseActivity<NoticePresenter> {
    @BindView(R.id.notice_toolbar)
    ToolBarLayout mNoticeToolbar;
    @BindView(R.id.notice_recyclerView)
    RecyclerView noticeRecyclerView;
    @BindView(R.id.notice_refresh)
    SmartRefreshLayout refreshLayout;


    private BaseAdapter<NoticeEntry.Res> adapter;

    private int page = 1;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_infor_mation;
    }

    @Override
    public NoticePresenter getPresenter() {
        return new NoticePresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        mNoticeToolbar.setLeftButtonOnClickLinster(v -> finish());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        noticeRecyclerView.setLayoutManager(linearLayoutManager);
        DividerItemDecoration divider = new DividerItemDecoration(mActivity, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.shape_item_decoration));
        noticeRecyclerView.addItemDecoration(divider);
        refreshLayout.setOnRefreshListener(refreshLayout1 -> {
            page = 1;
            mPresenter.getData(page);
        });
        refreshLayout.setOnLoadMoreListener(v -> {
            page++;
            mPresenter.getData(page);
        });
        ClassicsFooter classicsFooter = new ClassicsFooter(this);
        classicsFooter.setBackgroundColor(getResources().getColor(R.color.background));
        refreshLayout.setRefreshFooter(classicsFooter);
        ClassicsHeader classicsHeader = new ClassicsHeader(this);
        classicsHeader.setBackgroundColor(getResources().getColor(R.color.background));
        refreshLayout.setRefreshHeader(classicsHeader);
        adapter = new BaseAdapter<NoticeEntry.Res>(R.layout.recycler_item_notice, null, noticeRecyclerView) {
            @Override
            public void bind(ViewHolder holder, NoticeEntry.Res item) {
                holder.setText(R.id.notice_title, item.getTitle());
                holder.setText(R.id.notice_time, TimeFormatUtil.SF_FORMAT_E.format(item.getCreate_time()*1000));
                ClickUtil.click(holder.getView(R.id.notice_root), () -> WebViewActivity.start(mActivity, item.getId()));
            }
        };
        adapter.setEmtyView();
    }

    @Override
    public void initData() {
        mPresenter.getData(page);
    }


    public void stopRefresh() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
    }

    public void setData(NoticeEntry data) {
        if (data != null) {
            if (page == 1) {
                adapter.setNewData(data.getRes());
            } else {
                adapter.addData(data.getRes());
            }
        }

    }
}
